package com.hqyj.mall.xiaomimall.controller;

import com.hqyj.mall.xiaomimall.entity.OrderMain;
import com.hqyj.mall.xiaomimall.json.R;
import com.hqyj.mall.xiaomimall.service.OrderMainService;
import com.hqyj.mall.xiaomimall.vo.Cart;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderMainController {

    @Resource
    private OrderMainService orderMainService;
    //生成新的订单
    @PostMapping("/")
    public R saveNewOrder(@RequestBody Cart cart) {
        try {
            System.out.println("前端传过来的购物车对象是");
            System.out.println(cart);
            OrderMain orderMain= null;
            if( (orderMain = orderMainService.generateNewOrder(cart))!=null){
                return R.success("生成订单成功！",orderMain);
            }
            return R.fail("生成订单失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @PutMapping("/isPay/{oid}")
    public R modifyOrderPayStatus(@PathVariable("oid") String oid){
        try{
            OrderMain orderMain = null;
            if( (orderMain =orderMainService.modifyPayStatus(oid))!=null){
                return R.success("更新订单支付状态成功！",orderMain);
            }
            return R.fail("更新订单支付状态失败!");
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/query/{uid}")

    public R queryOrderMainListByUid(@PathVariable("uid") Integer uid){
        try{
            List<OrderMain> orderMainList = orderMainService.queryOrderMainListByUid(uid);
            return R.success("查询用户订单列表成功！",orderMainList);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
